<template>
  <div>
                                                                    <!-- 列表排序和搜索排序 -->
    <el-table
      :data="tableData.filter(data => !search || data.no.toLowerCase().includes(search.toLowerCase()))"
      style="width: 100%"
      height="504px"
      :stripe="true"
      :highlight-current-row="true"
    >
      <el-table-column type="index" :index="indexMethod"></el-table-column>      <!-- 索引 -->
      <el-table-column type="expand">                                            <!-- 详情展示 -->
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="支付状态">
              <span>{{ props.row.isPayed}}</span>
            </el-form-item>
            <el-form-item label="更新时间">
              <span>{{ props.row.updatedAt}}</span>
            </el-form-item>
            <el-form-item label="详细地址">
              <span>{{ props.row.regions + "-" +props.row.address}}</span>
            </el-form-item>
          </el-form>
        </template>        
      </el-table-column>
      <el-table-column label="订单 ID" prop="_id"></el-table-column>                 <!-- 列表展示 -->
      <el-table-column label="订单编号" prop="no"></el-table-column>
      <el-table-column label="价格" prop="price"></el-table-column>   
      <el-table-column align="right">                                                <!-- 搜索 -->    
        <template slot="header" slot-scope="scope">   
          <el-button type="danger" round class="add" @click="handAdd(scope.row)">+ 订单</el-button>       
          <el-input v-model="search" size="mini" placeholder="请输入订单名搜索" class="search"/>
        </template>
        <template slot-scope="scope">                                                <!-- 操作框操作 -->
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row) ">操作</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, tableData)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination                                      
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage1"
      :page-size="10"
      :background="true"
      layout="total,prev, pager, next, jumper"
      :total="this.arr.length||1"
    >                                                                                 <!-- 分页 -->
    </el-pagination>
    <el-dialog title="订单修改" :visible.sync="dialogFormVisible">                     <!-- 修改框 -->
      <el-form :model="form">
        <el-form-item label="商品分类" :label-width="formLabelWidth">
          <el-select v-model="form.isPayed" placeholder="请选择订单状态">
            <el-option label="true" value="true"></el-option>
            <el-option label="false" value="false"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
                                                                                      <!-- 修改框底部操作 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false;handsure()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import * as api from "@/api/getProlist";
export default {
  name: "proList",
  data() {
    return {
      list: [],
      currentPage1: 1,                      //初始页
      search: "",
      arr: [],
      tableData: [],
      dialogFormVisible: false,
      id: "",
      page: 1,                              //当前页
      ag: "",                               //当前条
      form: {
        isPayed:"",
      },
      formLabelWidth: "120px"
    };
  },
  methods: {
    indexMethod(index) {
      return index+(this.page-1)*10+1;
      },
    handleCurrentChange(val) {            //分页改变
      this.page = `${val}`;
      this.getarr(this.page - 1);
    },
    handleEdit(index, row) {              //修改订单
      this.dialogFormVisible = true;
      Object.assign(this.form, {
        isPayed: row.isPayed.value,
      });
      this.id = row._id;
      this.ag = index;
    },
    handsure() {                                                          //修改确认
      api.revOrder(this.id, this.form).then(data => {
        Object.assign(this.arr[this.ag + (this.page - 1) * 10], this.form); //页面上新数据替代老数据并展示新的数据
      });
    },
    handleDelete(index, row) {                                           //删除订单
      if(confirm("确认删除本订单?")){
      api.delOrder(row[index]._id).then(data => {});
      this.arr.splice(index + (this.page - 1) * 10, 1);
      row.splice(index, 1);
      }
    },
    getarr(index) {                                                     //分页函数封装
      this.tableData = [];
      index = index < 0 ? 0 : index > this.arr.length ? this.arr.length : index;
      for (var i = index * 10; i < (index + 1) * 10; i++) {
        this.tableData.push(this.arr[i]);
      }
    }
  },
  mounted() {
    api.getOrder({ per: 200 }).then(data => {
      console.log(data)              //获取订单列表
      data.data.orders.map((item, i) => {
        this.arr.push(item);
      });
      this.getarr(0);                   //默认第一页
    });
  }
};
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.el-main {
  overflow: visible;
}
.el-pagination {
  margin: 20px 0;
}
.pimg {
  width: 150px;
  height: 130px;
}
.add{
  float: left;
  width: 80px;
  height: 28px;
  text-indent: 4px;
  line-height: 3px;
  margin-top: 5px;
  font-size: 13px;
  margin-left: -25px
}
.search{
  float: right;
  width: 190px
}
</style>